<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
    <input type="file" name="file" id="file">
    <img src="" id="img" alt="">
    <button type="submit">上传</button>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
  <script>
      // 创建盒子 存储 文件
      var formData = new FormData();
      $('#file').change(function(){
        formData.append('file', this.files[0])
      })

      $('button').click(function(){
        // 提交
        $.ajax({
          url: '/upload',
          data: formData,
          type: 'post',
          processData: false, //必须 缓存关了 回显
          contentType: false, //必须 关闭 设置 数据格式（以 数据本身格式为准 xxxxx urlencoded）
          success: function(res){
            console.log(res);
            $('#img')[0].src=res.data.url
          }
        })
      })
  </script>
</body>
</html>